import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by ospip on 2017/11/28.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class Keyword extends JFrame {
    private String src = "D:\\keyword.txt";
    private String code = "D:\\code.txt";
    private JTextPane jt;
    private HTMLDocument textHTML;
    private HTMLEditorKit htmledit;
    private ArrayList<String> keyword;
    private JScrollPane js;
    public Keyword() {
        super("Keyword");
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        this.setSize(1500, 1000);
        this.setLayout(new BorderLayout());
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jt = new JTextPane();

        js = new JScrollPane(jt);
        this.add("Center", js);

        htmledit = new HTMLEditorKit();
        textHTML = (HTMLDocument) htmledit.createDefaultDocument();
        jt.setEditorKit(htmledit);
        jt.setContentType("text/html");
        jt.setDocument(textHTML);
        try {
            keyword = new ArrayList<>();
            FileReader fr = new FileReader(src);
            BufferedReader fin = new BufferedReader(fr);
            String line;
            while ((line = fin.readLine()) != null) {
                keyword.add(line);
                System.out.println(line);
            }
            fr.close();
            fin.close();
            fr = new FileReader(code);
            fin = new BufferedReader(fr);
            while ((line = fin.readLine()) != null) {
                String[] words = line.split(" ");
                for (String i : words) {
                    jt.setCaretPosition(jt.getDocument().getLength());
                    if (Arrays.binarySearch(keyword.toArray(), i) > 0) {
                        htmledit.insertHTML(textHTML, jt.getCaretPosition(),
                                "<font size=20 color=\"orange\">" + i + "&nbsp;</font>",
                                0, 0, HTML.Tag.FONT);
                    }
                    else {
                        htmledit.insertHTML(textHTML, jt.getCaretPosition(),
                                "<font size=20 color=\"black\">" + i + "&nbsp;</font>",
                                0, 0, HTML.Tag.FONT);
                    }
                }
                jt.setCaretPosition(jt.getDocument().getLength());
                htmledit.insertHTML(textHTML, jt.getCaretPosition(),
                        "<br>", 0, 0, HTML.Tag.BR);
            }
            fr.close();
            fin.close();
        }
        catch (Exception e) {}
    }

    public static void main(String[] args) {
        new Keyword().setVisible(true);
    }

}
